﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Plugins;

namespace MethodWorx.CMS.Content
{
    [Serializable]
    public class PageTemplatePropertyCollection : ChildEntityCollection<PageTemplate, Property>
    {
        public PageTemplatePropertyCollection(PageTemplate parent, Func<PageTemplate, IEnumerable<Property>> loader)
            : base(parent, loader)
        {
        }

        public void Add(string name, bool mandatory, string type, string group, ICustomPropertyDetails custom)
        {
            if(this.Where(ct => ct.Name.ToLower() == name.ToLower()).Any())
                throw new BusinessRuleException("Name '" + name + "' already exists");

            //
            //  ok, add it
            this.Add(new Property()
            {
                Name = name,
                Mandatory = mandatory,
                Group = group,
                Index = this.Count(),
                Type = type,
                Custom = custom
            });
        }

        public new void Clear()
        {
            base.Clear();
        }

        public void Remove(int index)
        {
            this.Remove(this.FirstOrDefault(p => p.Index == index));
        }
    }
}
